import java.util.ArrayList;
import java.util.List;

/**
 * @author SaoE
 * @date 2025/2/25 16:16
 */
public class _39 {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtrack(candidates, target, 0, new ArrayList<>());
        return res;
    }
    public void backtrack(int[] candidates, int target, int start, List<Integer> path) {
        if (target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = start; i < candidates.length; i++) {
            if (candidates[i] > target) {
                continue;
            }
            path.add(candidates[i]);
            backtrack(candidates, target - candidates[i], i, path);
            path.remove(path.size() - 1);
        }
    }
}
